/**
 * 
 */
package com.gr.staffpm.security.constants;

import java.util.ArrayList;
import java.util.List;

import com.gr.staffpm.datatypes.Permission;
import com.gr.staffpm.datatypes.Task;


/**
 * @author Graham Rhodes 3 May 2011 20:39:32
 */
public enum CRUDTaskPermission {
    ReadTask, UpdateTask, CommentOnTask;

    /* (non-Javadoc)
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        switch (this) {
            case ReadTask:
                return "Read Task";
            case UpdateTask:
                return "Update Task";
            case CommentOnTask:
                return "Comment On Task";
        }
        return super.toString();
    }

    public static List<CRUDTaskPermission> valuesAsList() {
        List<CRUDTaskPermission> permissions = new ArrayList<CRUDTaskPermission>();
        for (CRUDTaskPermission permission : values()) {
            permissions.add(permission);
        }
        return permissions;
    }

    public String getPermissionString(Task task) {
        switch (this) {
            case ReadTask:
                return "task:read:" + task.getTaskId();
            case UpdateTask:
                return "task:update:" + task.getTaskId();
            case CommentOnTask:
                return "task:comment" + task.getTaskId();
        }
        return "";
    }

    public static CRUDTaskPermission toPermission(Permission p, Task task) {
        if (p.getPermission().endsWith(":" + task.getTaskId())) {
            if (p.getPermission().contains(":read:")) {
                return ReadTask;
            } else if (p.getPermission().contains(":update:")) {
                return UpdateTask;
            } else if (p.getPermission().contains(":comment:")) {
                return CommentOnTask;
            }
        }
        return null;
    }
}
